<template>
  <div class="system-container">
    <vab-query-form>
      <el-form :inline="true" :model="queryForm" @submit.native.prevent>
        <el-form-item class="pr8">
          <el-select
            v-model="queryForm.machine"
            clearable
            filterable
            placeholder="机器类型"
            style="width: 200px"
          >
            <el-option
              v-for="(item, index) in machines"
              :key="index"
              :label="item.code"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="queryData">
            查询
          </el-button>
          <el-button icon="el-icon-plus" type="success" @click="handleEdit">
            添加
          </el-button>
        </el-form-item>
      </el-form>
    </vab-query-form>
    <el-table v-loading="listLoading" border :data="list">
      <el-table-column align="center" label="ID" prop="id" width="80" />
      <el-table-column
        align="center"
        label="机器类型"
        prop="machine"
        width="80"
      />
      <el-table-column
        align="center"
        label="机器名称"
        prop="name"
        width="140"
      />
      <el-table-column
        align="center"
        label="源文件夹"
        prop="raw"
        show-overflow-tooltip
        width="200"
      />
      <el-table-column
        align="center"
        label="拷贝文件夹"
        prop="copy"
        show-overflow-tooltip
        width="200"
      />
      <el-table-column
        align="center"
        label="目标文件夹"
        prop="target"
        show-overflow-tooltip
        width="200"
      />
      <el-table-column align="center" label="操作" width="140">
        <template #default="scope">
          <el-link
            class="mr12"
            icon="el-icon-edit"
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
          >
            修改
          </el-link>
          <el-link
            icon="el-icon-delete"
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :pagesize="queryForm.pagesize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getAll as getList, deleteOne } from '@/api/machine'
  import Edit from './components/edit'
  import { removeEmptyField } from '@/utils'
  import { machines } from '@/utils/constant'
  export default {
    name: 'InspectMachine',
    components: {
      Edit,
    },
    props: {
      activeName: {
        type: String,
        default() {
          return 'fourth'
        },
      },
    },
    data() {
      return {
        list: [],
        machines,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        listLoading: false,
        selectRows: '',
        queryForm: {
          machine: '',
          page: 1,
          pagesize: 10,
        },
        dialogVisible: false,
        bloodMatch: null,
        title: '',
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        this.$confirm('确定删除此条记录吗？', '操作确认', {
          type: 'warning',
        }).then(async () => {
          const { msg } = await deleteOne(row.id)
          this.$baseMessage(msg, 'success', 'vab-hey-message-success')
          this.queryData()
        })
      },
      queryData() {
        delete this.queryForm.page
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(removeEmptyField(this.queryForm))
        this.list = data.data
        this.total = data.total
        this.listLoading = false
      },
      handleRefresh() {
        this.fetchData()
      },
      handleSizeChange(val) {
        this.queryForm.pagesize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
    },
  }
</script>

<style lang="scss" scoped>
  .pr8 {
    padding-right: 8px;
  }
</style>
